package com.raymond.sgip.codec.encodeing;

import com.raymond.client.constants.GlobalConstants;
import com.raymond.client.msg.BaseMessage;
import com.raymond.client.utils.NettyByteBufUtil;
import com.raymond.cmpp.utils.CommonUtil;
import com.raymond.sgip.msg.request.ConnectRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * 连接请求编码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 15:18
 */
public class ConnectRequestMessageEncoder extends BaseSgipEncoder {

    @Override
    public byte[] encode0(BaseMessage msg) throws Exception {
        ConnectRequestMessage requestMessage = (ConnectRequestMessage)msg;
        ByteBuf bodyBuffer = Unpooled.buffer();
        bodyBuffer.writeByte(requestMessage.getLoginType());
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getLoginName().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                16));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getLoginPassowrd().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                16));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getReserve().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                8));
        return NettyByteBufUtil.toArray(bodyBuffer);
    }
}
